﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace ProjectEdificio
{
    public partial class MainWindow : Window
    {

        private Authentication auti = null;
        private BDManagment bdm = null;
        private ObservableCollection<Inquilino> dptos = null;
        private ObservableCollection<Cochera> cocheras = null;
        private ObservableCollection<Inquilino> allinquilinos = null;

        private ObservableCollection<Inquilino> dptosSearch = null;
        private ObservableCollection<Inquilino> allinquilinosSearch = null;

        public MainWindow()
        {
            InitializeComponent();
            
            auti = Authentication.getInstance();
            bdm = BDManagment.getInstance();

            this.dockPanelDptos.Visibility = Visibility.Collapsed;
            this.dockPanelCocheras.Visibility = Visibility.Collapsed;
            toolBarInquilinos.Visibility = Visibility.Collapsed;
            comboBoxSelectedVistas.IsEnabled = false;
            dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
        }



        private void comprobarExpensas()
        {
            ExpensaGneral expensa = bdm.getUltimaExpensasGneral();
            if (expensa == null)
            {
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                if (MessageBox.Show("Aún no se han ingresado al sistema los gastos de este mes, desea hacerlos ahora?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                {
                    DefinirExpensas definir = new DefinirExpensas(expensa);
                    definir.ShowDialog();
                }
            }
            else
            {
                DateTime fecha = expensa.FechaRedaccion;
                if (fecha.Month != DateTime.Now.Month)
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Aún no se han ingresado al sistema los gastos de este mes, desea hacerlos ahora?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        DefinirExpensas definir = new DefinirExpensas(expensa);
                        definir.ShowDialog();
                    }
                }
            }
            
        }

        private void closedSessionEvent()
        {
            this.dockPanelLogueo.Visibility = Visibility.Visible;
            auti.closedSession();
            closedSession.Visibility = Visibility.Collapsed;
            comboBoxSelectedVistas.IsEnabled = false;
            comboBoxSelectedVistas.SelectedItem = -1;
            this.dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
            this.dockPanelCocheras.Visibility = Visibility.Collapsed;
            this.dockPanelDptos.Visibility = Visibility.Collapsed;
            this.gridPageHome.Visibility = Visibility.Collapsed;
            this.loguinBar.Text = string.Empty;
            this.toolBarCocheras.Visibility = Visibility.Collapsed;
            this.toolBarInquilinos.Visibility = Visibility.Collapsed;
            this.toolBarInquilinosAll.Visibility = Visibility.Collapsed;
            this.menuItemgastosMensuales.IsEnabled = false;
            this.menuItemgenerateExpensas.IsEnabled = false;
            this.menuItemPreference.IsEnabled = false;
            this.menuItemUsersMenu.IsEnabled = false;
            this.toolBarFacturas.Visibility = Visibility.Collapsed;
            dockPanelFacturas.Visibility = Visibility.Collapsed;
            
        }

        private void buttonIngresar_Click(object sender, RoutedEventArgs e)
        {
            bool op = auti.setUser(textBoxUser.Text, textBoxPass.Password);
            if (op)
            {
                this.closedSession.Visibility = Visibility.Visible;
                this.loguinBar.Text = auti.getUser().User;
                this.dockPanelLogueo.Visibility = Visibility.Collapsed;
                comboBoxSelectedVistas.IsEnabled = true;
                comboBoxSelectedVistas.SelectedIndex = 0;
                this.gridPageHome.Visibility = Visibility.Visible;
                comprobarExpensas();

                this.menuItemgastosMensuales.IsEnabled = true;
                this.menuItemgenerateExpensas.IsEnabled = true;

                if (auti.getUser().Range == "Administrador")
                {
                    this.menuItemPreference.IsEnabled = true;
                    this.menuItemUsersMenu.IsEnabled = true;
                }


                this.textBoxUser.Text = string.Empty;
                this.textBoxPass.Password = string.Empty;
            }
            else
            {
                MessageBox.Show("Los datos no son correctos!");
            }
        }

        private void textBlockOlvidePass_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SendDatos s = new SendDatos();
            this.Visibility = Visibility.Hidden;
            s.ShowDialog();
            this.Visibility = Visibility.Visible;
        }

        

        private void MenuItem_Usuarios(object sender, RoutedEventArgs e)
        {
            Usuarios usuarios = new Usuarios();
            usuarios.Show();
            usuarios.Focus();
        }

        
        
        private void closeEvent(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void ButtonAddInquilino_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                if (inquia.Apellido == null)
                {
                    NewInquilino newInqui = new NewInquilino(inquia.NumPiso.ToString(), inquia.LetraPiso.ToString());
                    newInqui.ShowDialog();

                    dptos = bdm.getDptos();
                    listViewContenido.ItemsSource = dptos;
                }
                else
                {
                    MessageBox.Show("Tiene que estar desocupado el Departamento para asignar un nuevo Inquilino.");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Departamento.");
            }

            
        }

        private void ButtonClearDpto_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                
                if (inquia.Apellido != null)
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Desea eliminar al inquilino?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        bdm.deleteInquilino(inquia.NumPiso, inquia.LetraPiso);
                        dptos = bdm.getDptos();
                        listViewContenido.ItemsSource = dptos;
                    }
                }
                else
                {
                    MessageBox.Show("Tiene que estar ocupado el Departamento para eliminar a un Inquilino.");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Departamento.");
            }
            
        }

        private void ButtonEditInquilino_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                
                if (inquia.Apellido != null)
                {
                    NewInquilino newInqui = new NewInquilino(inquia);
                    newInqui.ShowDialog();
                    dptos = bdm.getDptos();
                    listViewContenido.ItemsSource = dptos;
                }
                else
                {
                    MessageBox.Show("Tiene que estar ocupado el Departamento para EDITAR un Inquilino.");
                }
                
            }
            else
            {
                MessageBox.Show("Selecciona un Departamento.");
            }
        }

        

        private void ButtonAddCochera_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                if (inquia.Apellido != null)
                {
                    if (inquia.NumCochera == 0)
                    {
                        NewCochera newco = new NewCochera(inquia.ID_Inquilino, inquia.Desde, inquia.Hasta);
                        newco.ShowDialog();
                        dptos = bdm.getDptos();
                        listViewContenido.ItemsSource = dptos;
                    }
                    else
                    {
                        MessageBox.Show("Este Inquilino ya tiene asignada una Cochera");
                    }
                    
                }
                else
                {
                    MessageBox.Show("Tiene que existir un Inquilino para AGREGAR una Cochera");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        

        private void ButtonAddService_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                if (inquia.Apellido != null)
                {
                    AddService add = new AddService(inquia.ID_Inquilino);
                    add.ShowDialog();
                }
                else
                {
                    MessageBox.Show("No se puede realizar la acción, ya que no existe un INQUILINO");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        private void gastosMensuales_Click(object sender, RoutedEventArgs e)
        {
            ExpensaGneral expensa = bdm.getUltimaExpensasGneral();
            DefinirExpensas definir = new DefinirExpensas(expensa);
            definir.ShowDialog();

        }

        //---------------------------------------------------------

        private void ComboBoxItem_SelectedHome(object sender, RoutedEventArgs e)
        {
            HomeView();
        }

        private void HomeView()
        {
            this.gridPageHome.Visibility = Visibility.Visible;

            this.dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
            this.dockPanelCocheras.Visibility = Visibility.Collapsed;
            this.dockPanelDptos.Visibility = Visibility.Collapsed;
            this.dockPanelLogueo.Visibility = Visibility.Collapsed;
            this.dockPanelFacturas.Visibility = Visibility.Collapsed;


            this.textBoxSearch.IsEnabled = false;
            this.textBoxSearch.Text = string.Empty;
            this.buttonSearch.IsEnabled = false;
            this.buttonSearch.Visibility = Visibility.Visible;
            this.buttonSearch2.Visibility = Visibility.Collapsed;

            this.toolBarCocheras.Visibility = Visibility.Collapsed;
            this.toolBarInquilinosAll.Visibility = Visibility.Collapsed;
            this.toolBarInquilinos.Visibility = Visibility.Collapsed;
            this.toolBarFacturas.Visibility = Visibility.Collapsed;
        }

        //----------------------------------------------------------

        private void ComboBoxItem_SelectedAllInquilinos(object sender, RoutedEventArgs e)
        {
            allInquilinosView();
        }

        private void allInquilinosView()
        {
            allinquilinos = bdm.getAllInquilinos();
            this.listViewAllInquilinos.ItemsSource = allinquilinos;

            this.dockPanelAllInquilinos.Visibility = Visibility.Visible;

            this.dockPanelCocheras.Visibility = Visibility.Collapsed;
            this.dockPanelDptos.Visibility = Visibility.Collapsed;
            this.dockPanelLogueo.Visibility = Visibility.Collapsed;
            this.gridPageHome.Visibility = Visibility.Collapsed;
            this.dockPanelFacturas.Visibility = Visibility.Collapsed;

            this.textBoxSearch.IsEnabled = true;
            this.textBoxSearch.Text = string.Empty;
            this.buttonSearch.IsEnabled = true;
            this.buttonSearch.Visibility = Visibility.Visible;
            this.buttonSearch2.Visibility = Visibility.Collapsed;

            this.toolBarCocheras.Visibility = Visibility.Collapsed;
            this.toolBarInquilinosAll.Visibility = Visibility.Visible;
            this.toolBarInquilinos.Visibility = Visibility.Collapsed;
            this.toolBarFacturas.Visibility = Visibility.Collapsed;
        }

        //----------------------------------------------------------

        private void ComboBoxItem_SelectedCocheras(object sender, RoutedEventArgs e)
        {
            cocherasView();
        }

        private void cocherasView()
        {
            dockPanelCocheras.Visibility = Visibility.Visible;
            cocheras = bdm.getListaCocheras();
            listViewContenidoCocheras.ItemsSource = cocheras;

            dockPanelDptos.Visibility = Visibility.Collapsed;
            dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
            this.dockPanelLogueo.Visibility = Visibility.Collapsed;
            this.gridPageHome.Visibility = Visibility.Collapsed;
            this.dockPanelFacturas.Visibility = Visibility.Collapsed;


            this.textBoxSearch.IsEnabled = false;
            this.textBoxSearch.Text = string.Empty;
            this.buttonSearch.IsEnabled = false;
            this.buttonSearch.Visibility = Visibility.Visible;
            this.buttonSearch2.Visibility = Visibility.Collapsed;

            this.toolBarCocheras.Visibility = Visibility.Visible;
            this.toolBarInquilinosAll.Visibility = Visibility.Collapsed;
            this.toolBarInquilinos.Visibility = Visibility.Collapsed;
            this.toolBarFacturas.Visibility = Visibility.Collapsed;
        }

        //----------------------------------------------------------

        private void ComboBoxItem_SelectedDptos(object sender, RoutedEventArgs e)
        {
            dptosView();
        }

        private void dptosView()
        {
            dockPanelDptos.Visibility = Visibility.Visible;
            dptos = bdm.getDptos();
            listViewContenido.ItemsSource = dptos;
            this.textBoxSearch.IsEnabled = true;
            this.buttonSearch.IsEnabled = true;

            dockPanelCocheras.Visibility = Visibility.Collapsed;
            this.gridPageHome.Visibility = Visibility.Collapsed;
            this.dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
            this.dockPanelLogueo.Visibility = Visibility.Collapsed;
            this.dockPanelFacturas.Visibility = Visibility.Collapsed;

            this.textBoxSearch.IsEnabled = true;
            this.textBoxSearch.Text = string.Empty;
            this.buttonSearch.IsEnabled = true;
            this.buttonSearch.Visibility = Visibility.Visible;
            this.buttonSearch2.Visibility = Visibility.Collapsed;

            this.toolBarCocheras.Visibility = Visibility.Collapsed;
            this.toolBarInquilinosAll.Visibility = Visibility.Collapsed;
            this.toolBarInquilinos.Visibility = Visibility.Visible;
            this.toolBarFacturas.Visibility = Visibility.Collapsed;
        }

        //----------------------------------------------------------

        private void facturasComboBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            facturasView();
        }

        private void facturasView()
        {
            dockPanelFacturas.Visibility = Visibility.Visible;
            listViewContenidoFacturas.ItemsSource = bdm.listFacturas();

            dockPanelCocheras.Visibility = Visibility.Collapsed;
            dockPanelDptos.Visibility = Visibility.Collapsed;
            dockPanelAllInquilinos.Visibility = Visibility.Collapsed;
            dockPanelLogueo.Visibility = Visibility.Collapsed;
            gridPageHome.Visibility = Visibility.Collapsed;


            this.textBoxSearch.IsEnabled = false;
            this.textBoxSearch.Text = string.Empty;
            this.buttonSearch.IsEnabled = false;
            this.buttonSearch.Visibility = Visibility.Visible;
            this.buttonSearch2.Visibility = Visibility.Collapsed;

            this.toolBarFacturas.Visibility = Visibility.Visible;
            this.toolBarCocheras.Visibility = Visibility.Collapsed;
            this.toolBarInquilinosAll.Visibility = Visibility.Collapsed;
            this.toolBarInquilinos.Visibility = Visibility.Collapsed;
        }

        //------------------------------------------------------------

        private void buttonLookDptos_Click(object sender, RoutedEventArgs e)
        {
            this.comboBoxSelectedVistas.SelectedIndex = 1;
            dptosView();
        }

        private void buttonLookFacture_Click(object sender, RoutedEventArgs e)
        {
            this.comboBoxSelectedVistas.SelectedIndex = 3;
            
        }

        private void buttonLookParking_Click(object sender, RoutedEventArgs e)
        {
            this.comboBoxSelectedVistas.SelectedIndex = 4;
            cocherasView();
        }

        private void closedSession_Click(object sender, RoutedEventArgs e)
        {
            this.closedSessionEvent();
        }

        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxSearch.Text != string.Empty)
            {
                this.buttonSearch.Visibility = Visibility.Collapsed;
                this.buttonSearch2.Visibility = Visibility.Visible;

                search();
            }
        }

        private void buttonSearch_Click2(object sender, RoutedEventArgs e)
        {
            this.buttonSearch2.Visibility = Visibility.Collapsed;
            this.buttonSearch.Visibility = Visibility.Visible;

            this.textBoxSearch.Text = string.Empty;
            clearSearch();
        }

        private void clearSearch()
        {
            switch (comboBoxSelectedVistas.SelectedIndex)
            {
                case 1:
                    dptosView();
                    break;
                case 2:
                    allInquilinosView();
                    break;
            }
        }

        private void search()
        {
            switch (comboBoxSelectedVistas.SelectedIndex)
            {
                case 1:
                    searchInDptos();
                    break;
                case 2:
                    searchInInquilinos();
                    break;
            }
        }

        private void searchInDptos()
        {
            if (textBoxSearch.Text != string.Empty)
            {
                dptosSearch = new ObservableCollection<Inquilino>();
                foreach (Inquilino inqui in listViewContenido.Items)
                {
                    if (inqui.DNI.ToString() == textBoxSearch.Text || inqui.ID_Inquilino.ToString() == textBoxSearch.Text || inqui.Apellido.ToLower() == textBoxSearch.Text.ToLower() || inqui.Nombre.ToLower() == textBoxSearch.Text.ToLower())
                    {
                        dptosSearch.Add(inqui);
                    }
                }
                this.listViewContenido.ItemsSource = dptosSearch;
            }
        }

        private void searchInInquilinos()
        {
            if (textBoxSearch.Text != string.Empty)
            {
                allinquilinosSearch = new ObservableCollection<Inquilino>();
                foreach (Inquilino inqui in listViewAllInquilinos.Items)
                {
                    if (inqui.DNI.ToString() == textBoxSearch.Text || inqui.ID_Inquilino.ToString() == textBoxSearch.Text || inqui.Apellido.ToLower() == textBoxSearch.Text.ToLower() || inqui.Nombre.ToLower() == textBoxSearch.Text.ToLower())
                    {
                        allinquilinosSearch.Add(inqui);
                    }
                }
                this.listViewAllInquilinos.ItemsSource = allinquilinosSearch;
            }
        }

        private void generateExpensas_Click(object sender, RoutedEventArgs e)
        {
            if (bdm.comprobateExpensas())
            {
                if (bdm.getPreferencias().PorDosPiezas != 0 && bdm.getPreferencias().PorTresPiezas != 0 && bdm.getPreferencias().PorUnaPieza != 0)
                {
                    if (bdm.getUltimaExpensasGneral() != null)
                    {
                        bdm.generateExpensas();
                    }
                }
                else
                {
                    MessageBox.Show("No se pueden generar las expensas porque faltan Preferencias.");
                }
            }
            else
            {
                MessageBox.Show("No se pueden generar las expensas porque faltan Gastos.");
            }
        }

        private void Preference_Click(object sender, RoutedEventArgs e)
        {
            Preferencias p = new Preferencias();
            p.ShowDialog();
        }

        private void buttonEmitirFacuraDpto_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                if (inquia.Apellido != null)
                {
                    FacturaWindows facture = new FacturaWindows(inquia);
                    facture.ShowDialog();
                    this.listViewContenido.ItemsSource = bdm.getDptos();
                }
                else
                {
                    MessageBox.Show("Tiene que existir un Inquilino!");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        private void buttonDeleteInquilinoAll_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;
                if (inquia.Activo == "Si")
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Desea eliminar al inquilino?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        bdm.deleteInquilino(inquia.NumPiso, inquia.LetraPiso);
                        allinquilinos = bdm.getAllInquilinos();
                        listViewAllInquilinos.ItemsSource = allinquilinos;
                    }
                }
                else
                {
                    MessageBox.Show("Para BORRAR un inquilino debe estar ACTIVO");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino.");
            }
        }

        private void buttonEditInquilinoAll_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;

                if (inquia.Activo == "Si")
                {
                    NewInquilino newInqui = new NewInquilino(inquia);
                    newInqui.ShowDialog();
                    allinquilinos = bdm.getAllInquilinos();
                    listViewAllInquilinos.ItemsSource = allinquilinos;
                }
                else
                {
                    MessageBox.Show("Tiene que estar ACTIVO el INQUILINO para EDITARLO.");
                }

            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino.");
            }
        }

        private void buttonAddCocheraAll_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;
                if (inquia.Activo == "Si")
                {
                    if (inquia.NumCochera == 0)
                    {
                        NewCochera newco = new NewCochera(inquia.ID_Inquilino, inquia.Desde, inquia.Hasta);
                        newco.ShowDialog();
                        allinquilinos = bdm.getAllInquilinos();
                        listViewAllInquilinos.ItemsSource = allinquilinos;
                    }
                    else
                    {
                        MessageBox.Show("Este Inquilino ya tiene asignada una Cochera");
                    }

                }
                else
                {
                    MessageBox.Show("Tiene que estar ACTIVO un Inquilino para ASIGNARLE una Cochera");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        private void buttonDeleteCocheraAll_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;
                if (inquia.Activo == "Si")
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Desea eliminar la Cochera?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        bdm.deleteCochera(inquia.ID_Inquilino);
                        allinquilinos = bdm.getAllInquilinos();
                        listViewAllInquilinos.ItemsSource = allinquilinos;
                    }
                }
                else
                {
                    MessageBox.Show("Tiene que estar OCUPADA la cochera para borrarla.");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino.");
            }
        }

        private void buttonAddServiceAll_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;
                if (inquia.Apellido != null)
                {
                    AddService add = new AddService(inquia.ID_Inquilino);
                    add.ShowDialog();
                }
                else
                {
                    MessageBox.Show("No se puede realizar la acción, ya que no existe un INQUILINO");
                }
                
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        private void buttonDeleteCocheraCoch_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenidoCocheras.SelectedItem != null)
            {
                Cochera coch = (Cochera)listViewContenidoCocheras.SelectedItem;
                if (coch.Apellido != null)
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Desea eliminar la Cochera?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        bdm.deleteCochera(coch.ID_Inquilino);
                        cocheras = bdm.getListaCocheras();
                        listViewContenidoCocheras.ItemsSource = cocheras;
                    }
                }
            }
            else
            {
                MessageBox.Show("Tiene que estar OCUPADA la cochera para borrarla.");
            }
        }

        private void buttonDeleteCochera_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenido.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewContenido.SelectedItem;
                if (inquia.NumCochera != 0)
                {
                    MessageBoxButton buttons = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Question;
                    if (MessageBox.Show("Desea eliminar la Cochera?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        bdm.deleteCochera(inquia.ID_Inquilino);
                        dptos = bdm.getDptos();
                        listViewContenido.ItemsSource = dptos;
                    }
                }
                else
                {
                    MessageBox.Show("Tiene que estar OCUPADA la cochera para borrarla.");
                }
            }
            else
            {
                MessageBox.Show("Selecciona una Cochera.");
            }
        }

        private void menuItemHelp_Click(object sender, RoutedEventArgs e)
        {
            HelpWIndows h = new HelpWIndows();
            h.ShowDialog();
        }

        private void buttonEmitirFacura_Click(object sender, RoutedEventArgs e)
        {
            if (listViewAllInquilinos.SelectedItem != null)
            {
                Inquilino inquia = (Inquilino)listViewAllInquilinos.SelectedItem;
                if (inquia.Apellido != null)
                {
                    FacturaWindows facture = new FacturaWindows(inquia);
                    facture.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Tiene que existir un Inquilino!");
                }
            }
            else
            {
                MessageBox.Show("Selecciona un Inquilino");
            }
        }

        private void buttonRemoveFacture_Click(object sender, RoutedEventArgs e)
        {
            if (listViewContenidoFacturas.SelectedItem != null)
            {
                MessageBoxButton buttons = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Question;
                if (MessageBox.Show("Desea eliminar la Factura?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                {
                    if (MessageBox.Show("Esta seguro?", "Confirmación", buttons, icon) == MessageBoxResult.Yes)
                    {
                        Factura fact = (Factura)listViewContenidoFacturas.SelectedItem;
                        bdm.removeFacture(fact.ID_Factura);
                        this.listViewContenidoFacturas.ItemsSource = bdm.listFacturas();
                    }
                }

            }
            else
            {
                MessageBox.Show("Debe seleccionar una factura para borrarla!");
            }
        }

    }
}